package com.leo.blog.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leo.blog.dao.VisitDao;
import com.leo.blog.dto.VisitExecution;
import com.leo.blog.entity.Visit;
import com.leo.blog.enums.VisitStateEnum;
import com.leo.blog.exceptions.VisitOperationException;
import com.leo.blog.service.VisitService;
import com.leo.blog.util.PageCalculator;

@Service
public class VisitServiceImpl implements VisitService{
	@Autowired
	private VisitDao visitDao;

	@Override
	public VisitExecution addVisit(Visit visit) throws VisitOperationException {
		if(visit != null) {
			//初始化创建时间
			visit.setCreateTime(new Date());
			
			int effectedNum = visitDao.insertVisit(visit);
			if(effectedNum > 0) {
				return new VisitExecution(VisitStateEnum.SUCCESS);
			}else {
				return new VisitExecution(VisitStateEnum.OFFLINE);//-1
			}
			
		}else {
			return new VisitExecution(VisitStateEnum.NULL_VISIT);
		}
	}

	@Override
	public VisitExecution addSelective(Visit visit) throws VisitOperationException {
		if(visit != null) {
			//初始化创建时间
			visit.setCreateTime(new Date());
			
			int effectedNum = visitDao.insertSelective(visit);
			if(effectedNum > 0) {
				return new VisitExecution(VisitStateEnum.SUCCESS);
			}else {
				return new VisitExecution(VisitStateEnum.OFFLINE);//-1
			}
			
		}else {
			return new VisitExecution(VisitStateEnum.NULL_VISIT);
		}
	}

	@Override
	public VisitExecution deleteVisitById(int visitId) {
		if(visitId > 0) {
			int effectedNum = visitDao.deleteVisitById(visitId);
			if(effectedNum > 0) {
				return new VisitExecution(VisitStateEnum.SUCCESS);
			}else {
				return new VisitExecution(VisitStateEnum.OFFLINE);//-1
			}
		}else {
			return new VisitExecution(VisitStateEnum.NULL_VISIT);
		}
	}

	@Override
	public VisitExecution updateVisit(Visit visit) {
		if(visit != null) {
			int effectedNum = visitDao.updateVisit(visit);
			if(effectedNum > 0) {
				return new VisitExecution(VisitStateEnum.SUCCESS);
			}else {
				return new VisitExecution(VisitStateEnum.OFFLINE);//-1
			}
		}else {
			return new VisitExecution(VisitStateEnum.NULL_VISIT);
		}
	}

	@Override
	public VisitExecution updateVisitSelect(Visit visit) {
		if(visit != null) {
			int effectedNum = visitDao.updateVisitSelect(visit);
			if(effectedNum > 0) {
				return new VisitExecution(VisitStateEnum.SUCCESS);
			}else {
				return new VisitExecution(VisitStateEnum.OFFLINE);//-1
			}
		}else {
			return new VisitExecution(VisitStateEnum.NULL_VISIT);
		}
	}

	@Override
	public VisitExecution getVisitById(int visitId) {
		if(visitId > 0) {
			Visit visit = visitDao.queryVisitById(visitId);
			if(visit != null) {
				return new VisitExecution(VisitStateEnum.SUCCESS, visit);
			}else {
				return new VisitExecution(VisitStateEnum.NULL_VISIT); 
			}
		}else {
			return new VisitExecution(VisitStateEnum.NULL_VISIT);
		}
	}

	@Override
	public VisitExecution getVisitByIp(String visitIp) {
		if(visitIp != null) {
			Visit visit = visitDao.queryVisitByIp(visitIp);
			if(visit != null) {
				return new VisitExecution(VisitStateEnum.SUCCESS, visit);
			}else {
				return new VisitExecution(VisitStateEnum.NULL_VISIT); 
			}
		}else {
			return new VisitExecution(VisitStateEnum.NULL_VISIT);
		}
	}

	@Override
	public VisitExecution getVisitByTimesCount(Visit visit) {
		if(visit != null) {
			Long timesCount = visitDao.queryVisitByTimesCount(visit);
			VisitExecution ve = new VisitExecution();
			if(timesCount > 0) {
				ve.setTimesCount(timesCount);
				ve.setState(VisitStateEnum.SUCCESS.getState());
				return ve;
			}else {
				return new VisitExecution(VisitStateEnum.OFFLINE);
			}
		}else {
			return new VisitExecution(VisitStateEnum.NULL_VISIT);
		}
	}

	@Override
	public VisitExecution getVisitListByPage(Visit visitCondition, int pageIndex, int pageSize) {
		VisitExecution ve = new VisitExecution();
		if(visitCondition != null) {
			//将页码转换成行码
			int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
			//根据查询条件,待用dao层返回相关类型列表
			List<Visit> visitList = visitDao.queryVisitListByPage(visitCondition, rowIndex, pageSize);
			int count = visitDao.queryVisitListByPageCount(visitCondition);
			
			if(visitList != null && count != 0) {
				ve.setVisitList(visitList);
				ve.setCount(count);
				ve.setState(VisitStateEnum.SUCCESS.getState());
			}else {
				ve.setState(VisitStateEnum.OFFLINE.getState());
			}
			
		}else {
			ve.setState(VisitStateEnum.NULL_VISIT.getState());
		}
		return ve;
	}

	@Override
	public VisitExecution getVisitListByGroupIp(Visit visitCondition, int pageIndex, int pageSize) {
		VisitExecution ve = new VisitExecution();
		if(visitCondition != null) {
			//将页码转换成行码
			int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
			//根据查询条件,待用dao层返回相关类型列表
			List<?> visitList = visitDao.queryVisitListByGroupIp(visitCondition, rowIndex, pageSize);
			int count = visitDao.queryVisitListByGroupIpCount(visitCondition);
			
			if(visitList != null && count != 0) {
				ve.setVisitList2(visitList);
				ve.setCount(count);
				ve.setState(VisitStateEnum.SUCCESS.getState());
			}else {
				ve.setState(VisitStateEnum.OFFLINE.getState());
			}
			
		}else {
			ve.setState(VisitStateEnum.NULL_VISIT.getState());
		}
		return ve;
	}

	@Override
	public VisitExecution getVisitListByDate(Visit visitCondition) {
		VisitExecution ve = new VisitExecution();
		if(visitCondition != null) {
			List<?> visitList = visitDao.queryVisitListByDate(visitCondition);
			ve.setVisitList2(visitList);
			ve.setState(VisitStateEnum.SUCCESS.getState());
		}else {
			ve.setState(VisitStateEnum.NULL_VISIT.getState());
		}
		return ve;
	}

	@Override
	public VisitExecution getVisitListByIp() {
		VisitExecution ve = new VisitExecution();		
		List<?> visitList = visitDao.queryVisitListByIp();
		if(visitList != null) {
			ve.setVisitList2(visitList);
			ve.setState(VisitStateEnum.SUCCESS.getState());
		}else {
			ve.setState(VisitStateEnum.OFFLINE.getState());
		}
		return ve;
	}

}
